package com.example.finger.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.finger.FingerHelper;
import com.example.finger.R;
import com.example.finger.bean.SoketOrder;
import com.example.finger.socket.SoketUploadData;
import com.example.finger.rules.FingerLib;
import com.google.gson.Gson;
import com.lyxmaster.lib.toasty.Toasty;
import com.lyxmaster.lib.util.DataConfig;
import com.lyxmaster.lib.view.BaseActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by LYX on 2018/11/15.
 */

public class FingerDialog extends BaseDialog {
    public TextView textViewTip;
    public TextView textViewTitle;
    private FingerLib m_szHost = FingerHelper.getInStance().m_szHost;
    private Context context;

    private int order = 0;
    private final int CLEANFINGER = 1;
    private final int INPUTFINGER = 2;
    private final int READFINGER = 3;

    @Override
    public View createView(Context context, LayoutInflater inflater, ViewGroup container) {
        super.createView(context,inflater,container);
        this.context = context;
        return inflater.inflate(R.layout.dialog_finger, container, false);
    }

    public static FingerDialog getInstance(String title) {
        FingerDialog dialogFragment = new FingerDialog();
        dialogFragment.setCanceledBack(false);
        dialogFragment.setCanceledOnTouchOutside(false);

        Bundle args = new Bundle();
        args.putString("title", title);
        dialogFragment.setArguments(args);

        return dialogFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        textViewTip = getView().findViewById(R.id.tv_tip);
        textViewTitle = getView().findViewById(R.id.tv_title);
        textViewTitle.setText(getArguments().getString("title"));
        textViewTip.setText("正在初始化");

        m_szHost.setOnFingerDoneListener(new FingerLib.OnFingerDoneListener() {
            @Override
            public void OnGetMessage(String content) {
                switch (order){
                    case CLEANFINGER:
                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                ((Activity)(context)).runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        order = INPUTFINGER;
                                        m_szHost.Run_CmdEnroll(FingerHelper.getInStance().CollectionId);
                                    }
                                });
                            }
                        },200);
                        break;
                    case INPUTFINGER:
                        if(content.startsWith("0")){
                            order = READFINGER;
                            m_szHost.Run_CmdReadTemplate(FingerHelper.getInStance().CollectionId);
                        }else {
                            Toasty.error(context, "录入失败"+ content).show();
                            textViewTip.setText("录入失败"+ content);
                            new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    ((Activity)(context)).runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            m_szHost.Run_CmdDeleteID(FingerHelper.getInStance().CollectionId);
                                            dismiss();
                                        }
                                    });
                                }
                            },2000);
                        }
                        break;
                    case READFINGER:
                        if(content.startsWith("0")){
                            SoketOrder soketOrder = new SoketOrder();
                            soketOrder.setResult(SoketOrder.CollectionFinger);
                            soketOrder.setData(DataConfig.hexByteToHexString(m_szHost.m_TemplateData, 570).replaceAll(" ", ""));
                            SoketUploadData.upload(new Gson().toJson(soketOrder).getBytes());
                            Toasty.success(context, "上传成功").show();
                        }else {
                            Toasty.error(context, "上传失败").show();
                        }
                        bShow = false;
                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                ((Activity)(context)).runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        m_szHost.Run_CmdDeleteID(FingerHelper.getInStance().CollectionId);
                                        dismiss();
                                    }
                                });
                            }
                        },2000);

                        break;
                }
            }
        });

        m_szHost.setOnFingerListener(new FingerLib.OnFingerListener() {
            @Override
            public void OnGetMessage(String content) {
                switch (order) {
                    case INPUTFINGER:
                        textViewTip.setText(content);
                        break;
                }
            }
        });

        order = CLEANFINGER;
        m_szHost.Run_CmdDeleteID(FingerHelper.getInStance().CollectionId);
    }

//    @Override
//    public void onDestroy(){
//        super.onDestroy();
//        m_szHost.setOnFingerListener(null);
//        m_szHost.setOnFingerDoneListener(null);
//        try{
//            m_szHost.Run_CmdCancel();
//        }catch(Exception e) {}
//    }

    @Override
    public void dismiss(){
        bShow = false;
        ((BaseActivity)(context)).hud.show("正在退出操作");


        m_szHost.setOnFingerListener(null);
        m_szHost.setOnFingerDoneListener(null);
//        m_szHost.setOnFingerDoneListener(new FingerLib.OnFingerDoneListener() {
//            @Override
//            public void OnGetMessage(String content) {
//                bFinishing = false;
//                ((BaseActivity) (context)).hud.dismiss();
//                m_szHost.setOnFingerDoneListener(null);
//                FingerDialog.super.dismiss();
//            }
//        });
        m_szHost.Run_CmdCancel();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                ((Activity)(getContext())).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((BaseActivity) (context)).hud.dismiss();
                        FingerDialog.super.dismiss();
                    }
                });
            }
        },2000);
    }
}
